from django.http import HttpResponse
from django.shortcuts import render
from django.views import View

# Create your views here.
from libs.yuntongxun.sms import CCP


class ImageCodeView(View):
    """图形验证码"""

    def get(self, request, uuid):
        """
        :param request: 请求对象
        :param uuid: 唯一标识图形验证码所属于的用户
        :return: image/jpeg
        """
        from libs.captcha.captcha import captcha
        # 生成图片验证码
        text, image = captcha.generate_captcha()
        from django_redis import get_redis_connection
        # 保存图片验证码
        redis_cli = get_redis_connection('code')
        redis_cli.setex(uuid, 100, text)

        # 响应图片验证码
        return HttpResponse(image, content_type='image/jpeg')


class SMSCodeView(View):
    """短信验证码"""

    def get(self, reqeust, mobile):
        """
        :param reqeust: 请求对象
        :param mobile: 手机号
        :return: JSON
        """
        # 接收参数
        image_code_client = reqeust.GET.get('image_code')
        uuid = reqeust.GET.get('image_code_id')

        # 校验参数
        from django import http
        if not all([image_code_client, uuid]):
            return http.JsonResponse({'code': 400, 'errmsg': '缺少必传参数'})

        # 创建连接到redis的对象
        from django_redis import get_redis_connection
        redis_conn = get_redis_connection('code')
        # 提取图形验证码
        image_code_server = redis_conn.get(uuid)
        if image_code_server is None:
            # 图形验证码过期或者不存在
            return http.JsonResponse({'code': 400, 'errmsg': '图形验证码失效'})
        # 对比图形验证码
        image_code_server = image_code_server.decode()  # bytes转字符串
        if image_code_client.lower() != image_code_server.lower():  # 转小写后比较
            return http.JsonResponse({'code': 400, 'errmsg': '输入图形验证码有误'})

        # 生成短信验证码：生成6位数验证码
        from random import randint
        sms_code = '%06d' % randint(0, 999999)

        # 保存短信验证码
        redis_conn.setex(mobile, 300, sms_code)
        # 发送短信验证码
        from libs.ronglian_sms_sdk import SmsSDK

        accId = '8aaf0708809721d001812ed2058333c6'
        accToken = '49f99e95df6a4c7f883e32b46493533e'
        appId = '8aaf0708809721d001812ed2066133cd'

        sdk = SmsSDK(accId, accToken, appId)
        data = (sms_code, '2')
        sdk.sendMessage('1', mobile, data)

        # sms_code_client = reqeust.GET.get('code')
        # if not all([sms_code_client]):
        #     return http.JsonResponse({'code': 400, 'errmsg': 'sms_code缺少必传参数'})
        # # 判断短信验证码是否正确：跟图形验证码的验证一样的逻辑
        # # 提取服务端存储的短信验证码：以前怎么存储，现在就怎么提取
        # redis_conn = get_redis_connection('code')
        # sms_code_server = redis_conn.get('sms_%s' % mobile)  # sms_code_server是bytes
        # # 判断短信验证码是否过期
        # if not sms_code_server:
        #     return http.JsonResponse({'code': 400, 'errmsg': '短信验证码失效'})
        # # 对比用户输入的和服务端存储的短信验证码是否一致
        # if sms_code != sms_code_server.decode():
        #     return http.JsonResponse({'code': 400, 'errmsg': '短信验证码有误'})

        # 响应结果
        return http.JsonResponse({'code': 0, 'errmsg': '发送短信成功'})
